<?php
/**
 * Project: ShopWorx- making your life easier
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @Author Chris
 * @File User.class.php
 * @Version 1.0.0
 * @Copyright 2008 Shopworx (c)
 * @Year 2008
 * @access Public
 */

class User
{
    function User($db = null, $uid = null)
    {

        $this->user_tbl = "users";
        $this->user_activity_log = "activity";
        $this->db = $db;

        $this->minimum_username_size = MIN_USERNAME_SIZE;
        $this->minimum_password_size = MIN_PASSWORD_SIZE;

        $this->USER_ID = $uid;

        $this->user_tbl_fields = array('EMAIL' => 'text', 'PASSWORD' => 'text', 'TYPE' =>
            'number', 'ACTIVE' => 'number');

        if (isset($this->USER_ID)) {
            $this->is_user = $this->getUserInfo();
        } else {
            $this->is_user = false;
        }
    }

    function isUser()
    {
        return $this->is_user;
    }

    function getUserId()
    {
        return $this->USER_ID;
    }

    function setUserId($uid = null)
    {
        if (!empty($uid)) {
            $this->USER_ID = $uid;
        }
        return $this->USER_ID;
    }

    function getUserIdByName($name = null)
    {
        if (!$name)
            return null;

        $stmt = "SELECT UID FROM $this->user_tbl WHERE EMAIL = '$name'";

        $result = $this->db->query($stmt);

        if ($result != null) {
            $row = $result->fetchRow();

            return $row->UID;
        }

        return null;
    }

    function getUserTypeList()
    {
        global $USER_TYPE;
        return $USER_TYPE;
    }

    function getUid()
    {
        return (isset($this->USER_ID)) ? $this->USER_ID : null;
    }

    function getEmail()
    {
        return (isset($this->EMAIL)) ? $this->EMAIL : null;
    }

    function getPassword()
    {
        return (isset($this->PASSWORD)) ? $this->PASSWORD : null;
    }

    function getActive()
    {
        return (isset($this->ACTIVE)) ? $this->ACTIVE : null;
    }

    function getType()
    {
        return (isset($this->TYPE)) ? $this->TYPE : null;
    }

    function getUserFieldList()
    {
        return array('UID', 'EMAIL', 'PASSWORD', 'ACTIVE', 'TYPE');
    }

    function getUserInfo($uid = null)
    {
        $fields = $this->getUserFieldList();

        $fieldstr = implode(',', $fields);
        $this->setUserId($uid);

        $stmt = "SELECT $fieldstr FROM $this->user_tbl" . "WHERE UID = $this->USER_ID";

        $result = $this->db->query($stmt);
		if($result != null){
        if ($result->numRows() > 0) {
            $row = $result->FetchRow();

            foreach ($fields as $f) {
                $this->$f = $row->$f;
            }

            return true;
        }
}
        return false;
    }

    function getUserIdByEmail($email = null)
    {
        $stmt = "SELECT UID FROM $this->user_tbl " . "WHERE EMAIL = '$email'";

        $result = $this->db->query($stmt);

        if ($result->numRows() > 0) {
            $row = $result->fetchRow();

            return $row->UID;
        } else {
            return 0;
        }
    }

    function getUserList()
    {
        $stmt = "SELECT UID, EMAIL FROM $this->user_tbl";

        $result = $this->db->query($stmt);

        $retArray = array();

        if ($result != null) {
            while ($row = $result->fetchRow()) {
                $retArray[$row->UID] = $row->EMAIL;
            }
        }

        return $retArray;
    }

    function makeKeyPair($fields = null, $data = null)
    {
        $setValues = array();

        while (list($k, $v) = each($fields)) {
            if (isset($data[$k])) {
                if (!strcmp($v, 'text')) {
                    $v = $this->db->quote(addslashes($data[$k]));

                    $setValues = "$k = $v";
                } else {
                    $setValues[] = "$k = $data[$k]";
                }
            }
        }
        return implode(', ', $setValues);
    }

    function updateUser($data = null)
    {
        $this->setUserId();

        $fieldList = $this->user_tbl_fields;

        $keyVal = $this->makeKeyPair($this->user_tbl_fields, $data);

        $stmt = "UPDATE $this->user_tbl SET $keyVal WHERE UID = $this->USER_ID";

        $result = $this->db->query($stmt);

        return $this->getReturnValue($result);
    }

    function addUser($data = null)
    {
        $fieldList = $this->user_tbl_fields;
        $valueList = array();

        while (list($k, $v) = each($fieldList)) {
            if (!strcmp($v, 'text')) {
                $valueList[] = $this->db->quote(addslashes($data[$k]));
            } else {
                $valueList[] = $data[$k];
            }
        }

        $fields = implode(',', array_keys($fieldList));
        $value = implode(',', $valueList);

        $stmt = "INSERT INTO $this->user_tbl ($fields) VALUES($values)";

        $result = $thid->db->query($result);

        return $this->getReturnValue($result);
    }

    function deleteUser($uid = null)
    {
        $this->setUserId($uid);

        $stmt = "DELETE from $this->user_tbl" . "WHERE UID = $this->USER_ID";

        $result = $this->db->query($stmt);

        return $this->getReturnValue($result);
    }

    function getReturnValue($r = null)
    {
        return ($r == DB_OK) ? true : false;
    }

    function writeLog($action = null)
    {
        $now = time();

        $stmt = "INSERT INTO $this->user_activity_log SET " . "UID = $this->USER_ID, " .
            "ACTION_TYPE = $action, " . "ACTION_TS = $now";
            
        $result = $this->db->query($stmt);
        
        return $this->getReturnValue($result);
    }
    
}
